/*
 *  Author: zhaijl 
 *  Url: http://218.1.73.212/down.php
 *  Country: China
 *  
 * ====================================================================
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * ====================================================================
 *
 * This software based apache mina 2.0 and spring 3.0
 *
 */
package org.cloudy.http.codec;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class Request {
    private Map< String, String[] > headers = null ;

	public void setHeaders( Map< String, String[] > headers ) {
        this.headers = headers ;
    }

    public Map< String, String[] > getHeaders () {
        return headers ;
    }

    public String getContext () {
        String[] context = ( String[] ) headers.get ( "Context" ) ;
        return context == null ? "" : context[0] ;
    }

    public String getParameter ( String name ) {
        String[] param = ( String[] ) headers.get ( "@".concat ( name ) ) ;
        return param == null || param.length <= 0 ? "" : param[0] ;
    }

    public String[] getParameters ( String name ) {
        String[] param = ( String[] ) headers.get ( "@".concat ( name ) ) ;
        return param == null ? new String[] {} : param ;
    }

    public String[] getHeader ( String name ) {
        return ( String[] ) headers.get ( name ) ;
    }

    public String toString () {
        StringBuilder str = new StringBuilder () ;

        Iterator< Entry< String, String[] > > it = headers.entrySet ().iterator () ;
        while ( it.hasNext () ) {
        	Entry< String, String[] > e = ( Entry< String, String[] > ) it.next () ;
            str.append ( e.getKey () + " : " + arrayToString ( ( String[] ) e.getValue (), ',' ) + "\n" ) ;
        }
        return str.toString () ;
    }

    public String arrayToString ( String[] s, char sep ) {
        if ( s == null || s.length == 0 )
            return "" ;
        StringBuffer buf = new StringBuffer () ;
        if ( s != null ) {
            for ( int i = 0 ; i < s.length ; i++ ) {
                if ( i > 0 ) buf.append ( sep ) ;
                buf.append ( s[i] ) ;
            }
        }
        return buf.toString () ;
    }
}
